package app.qrcode.browser.ykpixabay.ui.login

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.Navigation
import app.qrcode.browser.ykpixabay.R
import app.qrcode.browser.ykpixabay.logic.dao.KeyDao
import kotlinx.android.synthetic.main.fragment_enter_key.*

/**
 * Created by YiGo on 2022/4/17.
 */
class EnterKeyFragment : Fragment() {

    val viewModel by lazy { ViewModelProvider(this).get(PixViewModel::class.java) }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return inflater.inflate(R.layout.fragment_enter_key, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        enterKeyEditText.doOnTextChanged { text, _, _, _ ->
            viewModel.pixKey = text.toString()
        }
        enterBtn.setOnClickListener {
            if (enterKeyEditText.text.toString().isEmpty())
                Toast.makeText(context, "卡密不允许为空", Toast.LENGTH_SHORT).show()
            else {
                Log.w("cdm", "EnterKeyFragment.onActivityCreated -> ${viewModel.pixKey}")
                KeyDao.saveKey(viewModel.pixKey)
                val controller: NavController = Navigation.findNavController(it)
                controller.navigate(R.id.action_enterKeyFragment_to_viewPicFragment)
            }
        }
    }
}